package ibmetp.mis.email;

import ibmetp.mis.vo.User;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
/**
 * 邮件管理器
 * @author LAN DABIN
 *@create 2011-10-5
 */
public class EmailManager {
	/**
	 * @param args
	 */
	
	public static String getHost(String from)
	{
	  String[] arr=from.split("@");
	  return "smtp."+arr[arr.length-1];
	}
		
    public static String send(User user,String pwd,String to,String subject,String content)    
    {              
        
             
       
    	String result=null;
      
    	try{
   
    		  // Create the attachment
    		  EmailAttachment attachment = new EmailAttachment();
    		  //attachment.setPath("C:\\Users\\BIN\\Desktop\\Team\\project\\MIS\\WebContent\\WEB-INF\\classes\\ibmetp\\mis\\test\\SN.txt");
    		  attachment.setDisposition(EmailAttachment.ATTACHMENT);
    		  attachment.setDescription("Picture of John");
    		  attachment.setName("John.txt");
       
    		  // Create the email message
    		  MultiPartEmail email = new MultiPartEmail();
              email.setTLS(true);               
              email.setHostName(getHost(user.getEmail()));    
              System.out.println(getHost(user.getEmail()));
              email.setAuthentication(user.getEmail(), pwd);   //用户名和密码   
              String tos[]=to.split(",");
              for(int i=0;i<tos.length;i++)
              email.addTo(tos[i], "*");
              email.setFrom(user.getEmail(), user.getUserName());
    		  email.setSubject(subject);
    		  email.setMsg(content);  
    		  //email.attach(attachment);
    		  // send the email
    		SendEmailThread thread=new SendEmailThread(email);
    		thread.start();
      
          
    	}catch(Exception ex)
    	{  
    	  result=ex.getMessage();//ex.printStackTrace();
    	}
    	
    	return  result;
    }       
}
